﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using WPFCanvas;
using ExORMDesginer;
using ExORMDesginer.Helper;


namespace DiagramDesigner.Controls
{
    public class ChildExpandThumb : Thumb
    {
        public static DependencyProperty IsExpandedProperty = DependencyProperty.Register("IsExpanded",typeof(bool),typeof(ChildExpandThumb),new PropertyMetadata(true)); 
        public bool IsExpanded{
            set{
                SetValue(IsExpandedProperty,value);
            }
            get{
                return (bool)GetValue(IsExpandedProperty);
            }
        }

        public ChildExpandThumb()
        {  
            
            base.PreviewMouseDown += new System.Windows.Input.MouseButtonEventHandler(ChildThumb_PreviewMouseDown);
        }




        void ChildThumb_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DesignerItem designerItem = this.DataContext as DesignerItem;
            DesignerCanvas designer = VisualTreeHelper.GetParent(designerItem) as DesignerCanvas;
            if (designerItem != null && designer != null)
            {
                var ddd = designerItem.Template.FindName("PART_ConnectorDecorator", designerItem) as Control;
                var c = VisualTreeHelper.GetChildrenCount(ddd);

                var dd = ElementHelper.FindElementByTypes<Connector>(ddd);
                var c1 = dd[3];
                var con1 = c1.Connections[0];

                if (IsExpanded)
                {

                    con1.Visibility = System.Windows.Visibility.Hidden;
                    con1.Sink.ParentDesignerItem.Visibility = System.Windows.Visibility.Hidden;
                    IsExpanded = false;
                }
                else
                {
                    con1.Visibility = System.Windows.Visibility.Visible;
                    con1.Sink.ParentDesignerItem.Visibility = System.Windows.Visibility.Visible;
                    IsExpanded = true;
                }

            }
        }   
    }
}
